//
//  WDGoutPendingVehicleDetailViewModel.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2025/2/18.
//

#import "WDGoutPendingVehicleDetailViewModel.h"
#import "WDWorderOrdMainVehDetailServiceView.h"

@implementation WDGoutPendingVehicleDetailViewModel

- (void)initialize {
    [super initialize];
    self.title = @"工单详情";
}

- (void)parseDetailData {
    float imageWidth = (kScreenW - 20 * 2 - 16 * 2) / 3.0;
    float imageHeight = imageWidth / 1.42857;
    
    float width = kScreenW - 122 - 20;
    UIFont *font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    
    if (self.taskDetailModel.cancel) {
//        debugLog(@"cotent=%@", self.taskDetailModel.cancel.content);
        float tmpHeight = [objectNull(self.taskDetailModel.cancel.content) heightForFont:font width:width] + 2;
        tmpHeight = (tmpHeight < 18 ? 18 : tmpHeight);
        self.taskDetailModel.cancel.contentHeight = tmpHeight;
    }
    
    for (WDTaskFlowModel *flowModel in self.taskDetailModel.taskFlowList) {
        [flowModel.files removeAllObjects];
        [flowModel.otherFiles removeAllObjects];
        [flowModel.finishFiles removeAllObjects];
        
        flowModel.scanAddressHeight = [flowModel.scanAddress heightForFont:font width:width] + 2;
        if (flowModel.scanAddressHeight < 18) {
            flowModel.scanAddressHeight = 18;
        }
        flowModel.workExplainHeight = [flowModel.workExplain heightForFont:font width:width] + 2;
        if (flowModel.workExplainHeight < 18) {
            flowModel.workExplainHeight = 18;
        }
        
        if (flowModel.servOperate == 1) { // 接车
            self.taskDetailModel.meetCarFlowModel = flowModel;
        }
        else if (flowModel.servOperate == 2) { // 检查
            NSArray *types = @[@1, @2, @3, @4, @5];
            for (WDInspectUploadModel *img in flowModel.photoList) {
                // 上传类型 0默认 检查:1VIN码 2车辆仪表 3车头45度 4故障部位 5行驶证 6其他 维修:1新旧件对比 2车辆换件前 3车辆换件后 4其他 5完成
                if ([types containsObject:@(img.uploadType)]) {
                    [flowModel.files addObject:img];
                } else if (img.uploadType == 6) { // 其它
                    [flowModel.otherFiles addObject:img];
                }
            }
            for (WDInspectUploadModel *video in flowModel.videoList) {
                if ([types containsObject:@(video.uploadType)]) {
                    [flowModel.files addObject:video];
                } else if (video.uploadType == 6) { // 其它
                    [flowModel.otherFiles addObject:video];
                }
            }
            self.taskDetailModel.inspectFlowModel = flowModel;
        } else if (flowModel.servOperate == 4 || flowModel.servOperate == 5) { // 继续维修
            // 维修:1新旧件对比 2车辆换件前 3车辆换件后 4其他 5完成
            NSArray *types = @[@1, @2, @3];
            for (WDInspectUploadModel *img in flowModel.photoList) {
                if ([types containsObject:@(img.uploadType)]) {
                    [flowModel.files addObject:img];
                } else if (img.uploadType == 4) { // 其它
                    [flowModel.otherFiles addObject:img];
                } else if (img.uploadType == 5) { // 完成
                    [flowModel.finishFiles addObject:img];
                }
            }
            for (WDInspectUploadModel *video in flowModel.videoList) {
                if ([types containsObject:@(video.uploadType)]) {
                    [flowModel.files addObject:video];
                } else if (video.uploadType == 4) { // 其它
                    [flowModel.otherFiles addObject:video];
                } else if (video.uploadType == 5) { // 完成
                    [flowModel.finishFiles addObject:video];
                }
            }
            
            // 指定图片
            float imageTitleHeight = imageHeight + 5 + 18;
            flowModel.filesHeight = 0;
            if (flowModel.files.count > 0) {
                flowModel.filesHeight = 18 + 12 + imageTitleHeight;
            }
            
            // 其它图片
            if (flowModel.otherFiles.count == 0) {
                flowModel.otherFilesHeight = 0;
            } else {
                int row = ceilf(flowModel.otherFiles.count / 3.0);
                flowModel.otherFilesHeight = 10 + 18 + 12 + row * imageHeight + (row - 1) * 10;
            }
            
            // 完成图片
            if (flowModel.finishFiles.count == 0) {
                flowModel.finishFilesHeight = 0;
            } else {
                int row = ceilf(flowModel.finishFiles.count / 3.0);
                flowModel.finishFilesHeight = 10 + 18 + 12 + row * imageHeight + (row - 1) * 10;
            }
            
            flowModel.flowHeight = kWDTaskDetailBasicSingleViewHeight * 3 - 18 + flowModel.workExplainHeight + 9 + flowModel.filesHeight + (flowModel.filesHeight > 0 ? 18 : 0) + flowModel.otherFilesHeight + (flowModel.otherFilesHeight > 0 ? 0 : 0) + flowModel.finishFilesHeight + (flowModel.finishFilesHeight > 0 ? 0 : 0) + kWDTaskDetailBasicSingleViewHeight * 2 + 12;
            
            [self.taskDetailModel.tmpTaskFlows addObject:flowModel];
            
        }
    }
    
//    self.taskDetailModel.vehicleAds = @"河南省洛阳市朱雀大街与长兴街交口北350M左路口";
    self.taskDetailModel.vehicleAdsHeight = [self.taskDetailModel.vehicleAds heightForFont:font width:width] + 2;
    if (self.taskDetailModel.vehicleAdsHeight < 18) {
        self.taskDetailModel.vehicleAdsHeight = 18;
    }
    
    for (WDCanFaultInfoModel *ent in self.taskDetailModel.canFaultInfoList) {
        NSString *str = [NSDate timeStampWithString:ent.stimets format:@"yyyy-MM-dd HH:mm:ss"];
        ent.stimetsStr = str;
    }
    
    
    font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    width = kScreenW - 20 * 2 - 102;
    float height = [self.taskDetailModel.detail heightForFont:font width:width];
    height = (height > 18 ? height : 18);
    self.taskDetailModel.detailHeight = height;
    
    
    
    
    
    
    /*
    NSMutableArray *addList = [NSMutableArray new];
    WDBatteryInfoModel *model = [WDBatteryInfoModel new];
    model.batteryTypeValue = @"卡里锂电池"; // 电池类型
    WDBatteryModelModel *ent1 = [WDBatteryModelModel new];
    model.batteryModel = ent1;
    ent1.modelNo = @"KT2424242";// 型号
    [addList addObject:model];
    [addList addObject:model];

    self.taskDetailModel.batteryList = addList.copy;
    */
    
    self.taskDetailModel.imageWidth = imageWidth;
    self.taskDetailModel.imageHeight = imageHeight;
    self.taskDetailModel.inspectFlowModel.imageWidth = imageWidth;
    self.taskDetailModel.inspectFlowModel.imageHeight = imageHeight;
    NSInteger count = self.taskDetailModel.inspectFlowModel.otherFiles.count;
    if (count == 0) {
        self.taskDetailModel.inspectFlowModel.otherFilesHeight = 0;
    } else {
        int row = ceilf(count / 3.0);
        self.taskDetailModel.inspectFlowModel.otherFilesHeight = 10 + 18 + 12 + row * imageHeight + (row - 1) * 10;
    }
}

/// 获取详情
/// - Parameter completion: completion description
- (void)requestWithTaskInfoDetail:(RequestCompletionBlock)completion {
    @weakify(self);
    WDTaskInfoDetailRequest *request = [[WDTaskInfoDetailRequest alloc] initWithTaskId:@(self.serviceModel.id)];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        EDSRespondDataEntity *respond = request.responseModelObject;
        if (respond.code == EDS_NETWORK_SUCCESS) {
            self.taskDetailModel = respond.data;
            if (self.taskDetailModel.cancel && self.taskDetailModel.cancel.id == 0) {
                self.taskDetailModel.cancel = nil;
            }
            
            [self parseDetailData];
        }
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

@end
